home *** CD-ROM | disk | FTP | other *** search
/ Java Programmer's Toolkit / Java Programmer's Toolkit.iso / gs3.53 / impath.ps < prev    next >
Text File  |  1996-01-10  |  6KB  |  181 lines

  1. %    Copyright (C) 1992 Aladdin Enterprises.  All rights reserved.
  2. % This file is part of Aladdin Ghostscript.
  3. % Aladdin Ghostscript is distributed with NO WARRANTY OF ANY KIND.  No author
  4. % or distributor accepts any responsibility for the consequences of using it,
  5. % or for whether it serves any particular purpose or works at all, unless he
  6. % or she says so in writing.  Refer to the Aladdin Ghostscript Free Public
  7. % License (the "License") for full details.
  8. % Every copy of Aladdin Ghostscript must include a copy of the License,
  9. % normally in a plain ASCII text file named PUBLIC.  The License grants you
  10. % the right to copy, modify and redistribute Aladdin Ghostscript, but only
  11. % under certain conditions described in the License.  Among other things, the
  12. % License requires that the copyright notice and this notice be preserved on
  13. % all copies.
  14.  
  15. % impath.ps
  16. % Reverse-rasterize a bitmap to produce a Type 1 outline.
  17. % (This was formerly a Ghostscript operator implemented in C.)
  18.  
  19. %    <image> <width> <height> <wx> <wy> <ox> <oy> <string>
  20. %      type1imagepath <substring>
  21. %        Converts an image (bitmap) description of a character into
  22. %          a scalable description in Adobe Type 1 format.  The
  23. %          current transformation matrix should be the same as the
  24. %          FontMatrix of the font in which this character will be
  25. %          used: this establishes the scaling relationship between
  26. %          image pixels (the image is assumed to be 1 unit high in
  27. %          user space) and the character coordinate system used in
  28. %          the scalable description.  wx and wy are the character
  29. %          width, and ox and oy are the character origin relative
  30. %          to the lower left corner of the bitmap, in *pixels*.
  31. %        The image is assumed to be stored in left-to-right,
  32. %          top-to-bottom order.  Note that this is not consistent
  33. %          with the `image' operator's interpretation of the CTM.
  34. %        All coordinates in the scalable description are rounded to
  35. %          integers, so the coefficients in the FontMatrix should
  36. %          be on the order of 1/N for some value of N that is
  37. %          either a multiple of the height/width or is large
  38. %          compared to the width and height.  (There is a
  39. %          convention, which some PostScript programs rely on, that
  40. %          N=1000.)
  41. %        Note that the encoded description has *not* been subjected
  42. %          to CharString encryption, which is necessary before the
  43. %          description can be given to type1addpath: to do this,
  44. %          follow the type1imagepath with
  45. %            4330 exch dup .type1encrypt exch pop
  46. %        If the description is too complex to fit into the supplied
  47. %          string, a limitcheck error results.  A good rule of
  48. %          thumb is that the size of the string should be about 6
  49. %          times the number of 1-bits in the image that are not
  50. %          completely surrounded by other 1-bits.
  51.  
  52. % Import the Type 1 opcodes.
  53. (type1ops.ps) run
  54.  
  55. 100 dict
  56. dup /type1imagepath_dict exch def
  57. begin
  58.  
  59. /rc { round cvi } bind def
  60. /moving [/rmoveto /hmoveto /vmoveto] def
  61. /drawing [/rlineto /hlineto /vlineto] def
  62.  
  63. % Convert the current path to a Type 1 token array.
  64. /putxy            % x y ops -> cs_elements
  65.  { 3 -1 roll dup x sub rc exch /x exch def
  66.    3 -1 roll dup y sub rc exch /y exch def
  67.    % stack: ops dx dy
  68.    dup 0 eq
  69.     { % dy = 0, use hmoveto/lineto
  70.       pop exch 1 get
  71.     }
  72.     { 1 index 0 eq
  73.        { % dx = 0, use vmoveto/lineto
  74.          exch pop exch 2 get
  75.        }
  76.        { % use rmoveto/rlineto
  77.          3 -1 roll 0 get
  78.        }
  79.       ifelse
  80.     }
  81.    ifelse
  82.  } bind def
  83. /pathtotype1        % -> charstack
  84.  { 3 dict begin /x 0 def /y 0 def
  85.    mark
  86.    { moving putxy
  87.    }
  88.    { drawing putxy
  89.    }
  90.    { % Convert curve to relative form
  91.      x y 3
  92.       { exch neg 7 index add rc
  93.     exch neg 6 index add rc
  94.     8 -2 roll
  95.       }
  96.      repeat /y exch def /x exch def
  97.      1 index 0 eq 5 index 0 eq and    % dy1=dx3=0, hv
  98.       { 5 -1 roll pop exch pop /hvcurveto
  99.       }
  100.       { dup 0 eq 6 index 0 eq and    % dx1=dy3=0, vh
  101.          { 6 -1 roll pop pop /vhcurveto
  102.      }
  103.      { /rrcurveto            % none of the above
  104.      }
  105.     ifelse
  106.       }
  107.      ifelse
  108.    }
  109.    { /closepath
  110.    }
  111.    pathforall end
  112.  } bind def
  113.  
  114. end    % type1imagepath_dict
  115.  
  116. % The main program
  117. /type1imagepath        % image width height wx wy ox oy string ->
  118.             % substring
  119.  { type1imagepath_dict begin
  120.    /tsave save def
  121.    /ostring exch def
  122.    /oy exch def   /ox exch def
  123.    /wy exch def   /wx exch def
  124.    /height exch def   /width exch def
  125.    /data exch def
  126.  
  127.    /ofilter ostring /NullEncode filter def
  128.    /raster width 7 add 8 idiv def
  129.  
  130. % Construct the coordinate transformation.
  131.    height dup scale
  132.      matrix currentmatrix matrix invertmatrix setmatrix
  133.  
  134. % Determine the left side bearing.
  135.    /lsbx width
  136.    0 1 width 1 sub
  137.     { dup dup 8 idiv 0 exch
  138.       raster raster height mul 1 sub
  139.        { data exch get or }
  140.       for exch 8 mod bitshift 128 and 0 ne
  141.        { exch pop exit }
  142.       if pop
  143.     }
  144.    for def
  145.  
  146. % Compute and encode the origin, width, and side bearing.
  147.    mark
  148.    ox oy dtransform
  149.      rc /opty exch def   rc /optx exch def
  150.    wx wy dtransform
  151.      rc /iwy exch def    rc /iwx exch def
  152.    lsbx ox sub 0 dtransform
  153.      rc /ilsby exch def   rc /ilsbx exch def
  154.    ilsbx
  155.    iwy 0 ne ilsby 0 ne or
  156.     { ilsby iwx iwy /sbw }
  157.     { iwx /hsbw }
  158.    ifelse
  159.    ofilter charstack_write
  160.  
  161. % Flip over the Y axis, because the image is stored top-to-bottom.
  162.    [1 0 0 -1 0 height] concat
  163. % Account for the character origin.
  164.    lsbx oy translate
  165. % Trace the outline.
  166.    newpath
  167.    width height data .imagepath
  168.    gsave matrix setmatrix pathtotype1 grestore
  169.    ofilter charstack_write
  170. % Terminate the output
  171.    mark /endchar ofilter charstack_write
  172.  
  173.    ofilter fileposition ofilter closefile    % flush buffers
  174.    ostring 0 3 -1 roll getinterval
  175.    tsave restore
  176.    end
  177.  } bind def
  178.